﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace rtw
{
    static class FileHelper
    {
        public static List<string> GetFilesRecursive(string b)
        {
            // 1.
            // Store results in the file results list.
            List<string> result = new List<string>();

            // 2.
            // Store a stack of our directories.
            Stack<string> stack = new Stack<string>();

            // 3.
            // Add initial directory.
            stack.Push(b);

            // 4.
            // Continue while there are directories to process
            while (stack.Count > 0)
            {
                // A.
                // Get top directory
                string dir = stack.Pop();

                try
                {
                    // B
                    // Add all files at this directory to the result List.
                    result.AddRange(Directory.GetFiles(dir, "*.*"));

                    // C
                    // Add all directories at this directory.
                    foreach (string dn in Directory.GetDirectories(dir))
                    {
                        stack.Push(dn);
                    }
                }
                catch
                {
                    // D
                    // Could not open the directory
                }
            }
            return result;
        }
    }
    public class loader
    {
        public static List<entry> all(string dir)
        {
            var res = new List<entry>();

            List<string> dirs = FileHelper.GetFilesRecursive(dir);
            foreach (string p in dirs)
            {
                try
                {
                    List<entry> fe = load(p);
                    res.AddRange(fe);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }

            }

            res.Sort(delegate(entry a, entry b)
            {
                DateTime dt1 = DateTime.Parse(a.published);
                DateTime dt2 = DateTime.Parse(b.published);
                return dt1.CompareTo(dt2);
            });
            return res;
        }
        public static List<entry> load(string filename)
        {
            string[] text=System.IO.File.ReadAllLines(filename);
            var res = new List<entry>();
            foreach (var s in text)
            {
                entry e = new entry();

                string[] parts=s.Split(',');
                e.published = parts[0];
                
                e.author = parts[1];
                e.twitid = parts[2];
                e.title = parts[3];
                e.title_lowered = e.title.ToLower();
                res.Add(e);

                e.convertPublishedToTime();
            }
            return res;
        }

    }
}
